#include "MainWindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->graphicsView->scene->setSceneRect(ui->graphicsView->rect());
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    ui->graphicsView->AddArc(ui->graphicsView->start,ui->graphicsView->end);
    ui->graphicsView->init();
    ui->graphicsView->start=nullptr;
    ui->graphicsView->end=nullptr;
}

void MainWindow::on_pushButton_3_clicked()
{
    qreal Z=QInputDialog::getDouble(this,"InputZ","Z");
    for(int i=0;i<ui->graphicsView->EdgeCount;i++)
        if(ui->graphicsView->edge[i]->start==ui->graphicsView->start&&ui->graphicsView->edge[i]->end==ui->graphicsView->end){
            ui->graphicsView->edge[i]->Z=Z;
            ui->graphicsView->edge[i]->update();
        }
    ui->graphicsView->init();
    ui->graphicsView->start=nullptr;
    ui->graphicsView->end=nullptr;
}

void MainWindow::on_pushButton_4_clicked()
{
    ui->graphicsView->UpdateI_s();
    qreal I=QInputDialog::getDouble(this,"InputI","I");
    for(int i=0;i<ui->graphicsView->EdgeCount;i++)
        if(ui->graphicsView->edge[i]->start==ui->graphicsView->start&&ui->graphicsView->edge[i]->end==ui->graphicsView->end)
            ui->graphicsView->edge[i]->I=I;
    ui->graphicsView->UpdateI_s();
    ui->graphicsView->init();
    ui->graphicsView->start=nullptr;
    ui->graphicsView->end=nullptr;
}

void MainWindow::on_pushButton_5_clicked()
{
    ui->graphicsView->UpdateU_s();
    qreal U=QInputDialog::getDouble(this,"InputU","U");
    for(int i=0;i<ui->graphicsView->EdgeCount;i++)
        if(ui->graphicsView->edge[i]->start==ui->graphicsView->start&&ui->graphicsView->edge[i]->end==ui->graphicsView->end)
            ui->graphicsView->edge[i]->U=U;
    ui->graphicsView->UpdateU_s();
    ui->graphicsView->init();
    ui->graphicsView->start=nullptr;
    ui->graphicsView->end=nullptr;
}

void MainWindow::on_pushButton_6_clicked()
{
    ui->A->setRowCount(Vertex::VertexCount-1);
    ui->A->setColumnCount(ui->graphicsView->EdgeCount);
    ui->graphicsView->UpdateA();
    for (int i = 0; i < Vertex::VertexCount-1; ++i) {
        for (int j = 0; j < ui->graphicsView->EdgeCount; ++j) {
            qDebug()<<ui->graphicsView->A(i,j);
            QTableWidgetItem* item = new QTableWidgetItem(QString::number(ui->graphicsView->A(i,j)));
            ui->A->setItem(i, j, item);
        }
    }
    ui->G_n->setRowCount(Vertex::VertexCount-1);
    ui->G_n->setColumnCount(Vertex::VertexCount-1);
    ui->graphicsView->UpdateG();
    for(int i=0;i<Vertex::VertexCount-1;i++)
        for(int j=0;j<Vertex::VertexCount-1;j++){
            QTableWidgetItem* item=new QTableWidgetItem(QString::number(ui->graphicsView->G_n(i,j)));
            ui->G_n->setItem(i,j,item);
        }
    ui->U_n->setRowCount(Vertex::VertexCount-1);
    ui->U_n->setColumnCount(1);
    ui->graphicsView->solve();
    for(int i=0;i<Vertex::VertexCount-1;i++){
        QTableWidgetItem* item=new QTableWidgetItem(QString::number(ui->graphicsView->U_n(i,0)));
        ui->U_n->setItem(i,0,item);
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    ui->graphicsView->DelArc(ui->graphicsView->HasConnection(ui->graphicsView->start,ui->graphicsView->end));
    ui->graphicsView->init();
    ui->graphicsView->start=nullptr;
    ui->graphicsView->end=nullptr;
}

void MainWindow::on_pushButton_7_clicked()
{
    ui->graphicsView->ParSim();
}
